using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Text;

public class DatabaseGenerating : MonoBehaviour
{
    public Button btn;
    private string filepath1;
    private string filepath2;
    private string filepath3;
    private string filepath4;

    // Start is called before the first frame update
    void Start()
    {
        filepath1 = @"C:\Users\caomb\Desktop\实训\地图路径.txt";
        filepath2 = @"C:\Users\caomb\Desktop\实训\代表点路径.txt";
        filepath3 = @"C:\Users\caomb\Desktop\实训\数据库.txt";
        filepath4 = @"C:\Users\caomb\Desktop\实训\数据库索引.txt";
        btn.onClick.AddListener(foo);
    }

    void foo()
    {
        string[] path = File.ReadAllLines(filepath2);
        ArrayList index = new ArrayList();
        int count = 0;
        for(int i = 0; i < path.Length; i+=2) {
            count++;
            if(i == path.Length-2) {
                index.Add(count);
                count = 0;
            } else {
                string[] tmp1 = path[i].Split();
                string[] tmp2 = path[i+2].Split();
                if((int)Convert.ToDouble(tmp1[0]) != (int)Convert.ToDouble(tmp2[0])) {
                    index.Add(count);
                    count = 0;
                }
            }
        }
        index.Add(0);
        int[,] _index = new int[index.Count, index.Count];
        for(int i = 0; i < index.Count; i++) {
            for(int j = 0; j < index.Count; j++) {
                _index[i, j] = -1;
            }
        }
        int row = 0;
        string[] _path = File.ReadAllLines(filepath1);
        using(StreamWriter sw = new StreamWriter(filepath3, false)) {
            for(int i = 0; i < _path.Length; i+=2) {
                string[] tmp = _path[i].Split();
                int region1 = (int)Convert.ToDouble(tmp[0]);
                int region2 = (int)Convert.ToDouble(tmp[1]);
                sw.WriteLine(region1 + " " + region2);
                row++;
                _index[region1, region2] = row;
                string[] _tmp = _path[i+1].Split();
                for(int j = 0; j < _tmp.Length-1; j++) {
                    int k = j + 1;
                    int _region1 = (int)Convert.ToDouble(_tmp[j]);
                    int _region2 = (int)Convert.ToDouble(_tmp[k]);
                    count = 0;
                    int h = 0;
                    for(h = 0; h < _region1; h++) {
                        count += (int)index[h] * 2;
                    }
                    for(h = 0; h < ((int)index[_region1])*2; h+=2) {
                        string[] __tmp = path[count+h].Split();
                        if(_region2 == (int)Convert.ToDouble(__tmp[1])) {
                            break;
                        }
                    }
                    string[] fullPath = path[count+h+1].Split();
                    for(h = 0; h < fullPath.Length-1; h++) {
                        if(h == 0 && j != 0) {
                            continue;
                        }
                        sw.Write(fullPath[h]);
                        if(j != _tmp.Length-2 || h != fullPath.Length-2) {
                            sw.Write(" ");
                        }
                    }
                }
                sw.Write("\n");
            }
        }
        using(StreamWriter sw = new StreamWriter(filepath4, false)) {
            for(int i = 0; i < index.Count; i++) {
                for(int j = 0; j < index.Count; j++) {
                    sw.Write(_index[i, j]);
                    sw.Write(" ");
                }
                sw.Write("\n");
            }
        }
    }

    // void foo()
    // {
    //     using(StreamWriter sw1 = new StreamWriter(filepath3, false)) {
    //         using(StreamReader sr1 = new StreamReader(filepath1, Encoding.UTF8)) {
    //             string line;
    //             string[] tmp;
    //             int time = 0;
    //             while(sr1.Peek() >= 0) {
    //                 time++;
    //                 line = sr1.ReadLine();
    //                 if(line.Length > 0) {
    //                     tmp = line.Split();
    //                     int region1 = (int)Convert.ToDouble(tmp[0]);
    //                     int region2 = (int)Convert.ToDouble(tmp[1]);
    //                     line = sr1.ReadLine();
    //                     tmp = line.Split();
    //                     ArrayList path = new ArrayList();
    //                     for(int i = 0; i < tmp.Length-1; i++) {
    //                         int j = i + 1;
    //                         int _region1 = (int)Convert.ToDouble(tmp[i]);
    //                         int _region2 = (int)Convert.ToDouble(tmp[j]);
    //                         using(StreamReader sr2 = new StreamReader(filepath2, Encoding.UTF8)) {
    //                             string _line;
    //                             string[] _tmp;
    //                             while(sr2.Peek() >= 0) {
    //                                 _line = sr2.ReadLine();
    //                                 if(_line.Length > 0) {
    //                                     _tmp = _line.Split();
    //                                     int __region1 = (int)Convert.ToDouble(_tmp[0]);
    //                                     int __region2 = (int)Convert.ToDouble(_tmp[1]);
    //                                     if(__region1 != _region1 || __region2 != _region2) {
    //                                         sr2.ReadLine();
    //                                         continue;
    //                                     } else {
    //                                         string __line = sr2.ReadLine();;
    //                                         string[] __tmp = __line.Split();
    //                                         for(int k = 0; k < __tmp.Length-1; k++) {
    //                                             if(k == 0 && i != 0) {
    //                                                 continue;
    //                                             }
    //                                             path.Add(ConvertToPoint(__tmp[k]));
    //                                         }
    //                                     }
    //                                 }
    //                             }
    //                         }
    //                     }
    //                     ArrayList _path = new ArrayList();
    //                     Vector3Int p = (Vector3Int)path[0];
    //                     _path.Add(p);
    //                     while(p != (Vector3Int)path[path.Count-1]) {
    //                         p = BinarySearch(path, p);
    //                         _path.Add(p);
    //                     }
    //                     sw1.WriteLine(region1 + " " + region2);
    //                     for(int i = 0; i < _path.Count; i++) {
    //                         p = (Vector3Int)_path[i];
    //                         sw1.Write("(");
    //                         sw1.Write(p.x);
    //                         sw1.Write(",");
    //                         sw1.Write(p.y);
    //                         sw1.Write(")");
    //                         sw1.Write(" ");
    //                     }
    //                     sw1.Write("\n");
    //                 }
    //             }
    //         }
    //     }
    // }

    // Vector3Int ConvertToPoint(string tmp) {
    //     char[] separator = {'(', ',', ')'};
    //     string[] _tmp = tmp.Split(separator);
    //     int x = (int)Convert.ToDouble(_tmp[1]);
    //     int y = (int)Convert.ToDouble(_tmp[2]);
    //     Vector3Int p = new Vector3Int(x, y, 0);
    //     return p;
    // }

    // Vector3Int BinarySearch(ArrayList path, Vector3Int p) {
    //     int low = path.IndexOf(p), high = path.Count-1, mid = 0;
    //     while(low <= high) {
    //         mid = (low + high) / 2;
    //         if(Reachable(p, (Vector3Int)path[mid])) {
    //             low = mid + 1;
    //         } else {
    //             high = mid - 1;
    //         }
    //     }
    //     Vector3Int _p = (Vector3Int)path[mid];
    //     return _p;
    // }

    // bool Reachable(Vector3Int p1, Vector3Int p2) {
    //     Vector3Int p = p1;
    //     Vector3Int rep = p2;
    //     while(p != rep) {
    //         ArrayList list = new ArrayList();
    //         Vector3Int next = p;
    //         double f = 999999;
    //         for(int i = -1; i <= 1; i++) {
    //             for(int j = -1; j <= 1; j++) {
    //                 Vector3Int tmp = new Vector3Int(p.x+i, p.y+j, 0);
    //                 if(map.HasTile(tmp)) {
    //                     list.Add(tmp);
    //                 }
    //             }
    //         }
    //         double g = 0, h = 0;
    //         int offsetX = Math.Abs(rep.x - p.x);
    //         int offsetY = Math.Abs(rep.y - p.y);
    //         double square = offsetX * offsetX + offsetY * offsetY;
    //         double current_h = Math.Pow(square, 0.5);
    //         for(int i = 0; i < list.Count; i++) {
    //             Vector3Int tmp = (Vector3Int)list[i];
    //             offsetX = Math.Abs(p.x - tmp.x);
    //             offsetY = Math.Abs(p.y - tmp.y);
    //             square = offsetX * offsetX + offsetY * offsetY;
    //             g = Math.Pow(square, 0.5);
    //             offsetX = Math.Abs(rep.x - tmp.x);
    //             offsetY = Math.Abs(rep.y - tmp.y);
    //             square = offsetX * offsetX + offsetY * offsetY;
    //             h = Math.Pow(square, 0.5);
    //             if(current_h > h && f > g + h) {
    //                 next = tmp;
    //                 f = g + h;
    //             }
    //         }
    //         if(p == next) {
    //             if(p != rep) {
    //                 return false;
    //             } else {
    //                 return true;
    //             }
    //         } else {
    //             p = next;
    //             if(p == rep) {
    //                 return true;
    //             }
    //         }
    //     }
    //     return false;
    // }

    // Update is called once per frame
    void Update()
    {
        
    }
}
